const axios = require('axios');

(async () => {
  console.log('🔍 深入调试勋章筛选和计数问题');
  
  try {
    // 1. 登录用户
    const loginResponse = await axios.post('http://localhost:3000/api/auth/login', {
      email: 'admin@campus.com',
      password: 'admin123'
    });
    
    const authToken = loginResponse.data.token;
    const userId = loginResponse.data.user.id;
    console.log(`✅ 用户登录成功: ID=${userId}`);
    
    // 2. 获取各类勋章
    console.log('\n🏆 获取勋章数据...');
    
    // "全部勋章"
    const allBadgesResponse = await axios.get('http://localhost:3000/api/badges', {
      headers: { Authorization: `Bearer ${authToken}` }
    });
    const allBadges = allBadgesResponse.data.badges;
    
    // "我的勋章"
    const ownedBadgesResponse = await axios.get('http://localhost:3000/api/badges?filter=owned', {
      headers: { Authorization: `Bearer ${authToken}` }
    });
    const ownedBadges = ownedBadgesResponse.data.badges;
    
    // "可获得勋章"
    const availableBadgesResponse = await axios.get('http://localhost:3000/api/badges?filter=available', {
      headers: { Authorization: `Bearer ${authToken}` }
    });
    const availableBadges = availableBadgesResponse.data.badges;
    
    // 3. 计数和验证
    console.log('\n📊 勋章数量:');
    console.log(`全部勋章: ${allBadges.length}个`);
    console.log(`我的勋章: ${ownedBadges.length}个`);
    console.log(`可获得勋章: ${availableBadges.length}个`);
    
    // 4. 分析重叠
    console.log('\n🔍 勋章ID分析:');
    
    // 提取ID
    const allIds = allBadges.map(badge => badge.id);
    const ownedIds = ownedBadges.map(badge => badge.id);
    const availableIds = availableBadges.map(badge => badge.id);
    
    console.log(`全部勋章ID数量: ${allIds.length}`);
    console.log(`我的勋章ID数量: ${ownedIds.length}`);
    console.log(`可获得勋章ID数量: ${availableIds.length}`);
    
    // 检查我的勋章中是否有ID在可获得勋章中出现
    const overlapIds = ownedIds.filter(id => availableIds.includes(id));
    console.log(`重叠的勋章ID数量: ${overlapIds.length}`);
    if (overlapIds.length > 0) {
      console.log(`重叠的ID: ${overlapIds.join(', ')}`);
    }
    
    // 检查全部勋章中是否包含所有"我的勋章"
    const ownedNotInAll = ownedIds.filter(id => !allIds.includes(id));
    console.log(`"我的勋章"中不在"全部勋章"中的数量: ${ownedNotInAll.length}`);
    if (ownedNotInAll.length > 0) {
      console.log(`不在"全部勋章"中的"我的勋章"ID: ${ownedNotInAll.join(', ')}`);
    }
    
    // 检查全部勋章中是否包含所有"可获得勋章"
    const availableNotInAll = availableIds.filter(id => !allIds.includes(id));
    console.log(`"可获得勋章"中不在"全部勋章"中的数量: ${availableNotInAll.length}`);
    if (availableNotInAll.length > 0) {
      console.log(`不在"全部勋章"中的"可获得勋章"ID: ${availableNotInAll.join(', ')}`);
    }
    
    // 5. 比较勋章内容
    console.log('\n📝 勋章内容分析:');
    
    // 检查有多少个勋章是"potential_"开头的(未获得的)
    const potentialInAll = allBadges.filter(badge => String(badge.id).startsWith('potential_')).length;
    const potentialInOwned = ownedBadges.filter(badge => String(badge.id).startsWith('potential_')).length;
    const potentialInAvailable = availableBadges.filter(badge => String(badge.id).startsWith('potential_')).length;
    
    console.log(`"全部勋章"中未获得勋章(potential_)数量: ${potentialInAll}`);
    console.log(`"我的勋章"中未获得勋章(potential_)数量: ${potentialInOwned}`);
    console.log(`"可获得勋章"中未获得勋章(potential_)数量: ${potentialInAvailable}`);
    
    // 获取活动总数
    try {
      const activitiesResponse = await axios.get('http://localhost:3000/api/activities');
      const totalActivities = activitiesResponse.data.activities.length;
      console.log(`\n🎪 活动总数: ${totalActivities}`);
      
      // 分析活动和勋章的关系
      const activityIds = activitiesResponse.data.activities.map(activity => activity.id);
      const badgeActivityIds = allBadges.map(badge => badge.activity_id).filter(id => id);
      const uniqueBadgeActivityIds = [...new Set(badgeActivityIds)];
      
      console.log(`勋章中包含的活动ID数量: ${uniqueBadgeActivityIds.length}`);
      console.log(`没有关联勋章的活动数量: ${activityIds.length - uniqueBadgeActivityIds.length}`);
    } catch (error) {
      console.log('无法获取活动数据:', error.message);
    }
    
    // 6. 分析特定勋章
    if (ownedBadges.length > 0) {
      console.log('\n🔎 我的勋章示例:');
      const ownedExample = ownedBadges[0];
      console.log(JSON.stringify(ownedExample, null, 2));
    }
    
    if (availableBadges.length > 0) {
      console.log('\n🔎 可获得勋章示例:');
      const availableExample = availableBadges[0];
      console.log(JSON.stringify(availableExample, null, 2));
    }
    
    // 7. 结论与修复建议
    console.log('\n📋 问题诊断与修复建议:');
    if (allBadges.length < ownedBadges.length + availableBadges.length) {
      console.log('❌ "全部勋章"数量小于"我的勋章"+"可获得勋章"');
      console.log('   可能原因: "全部勋章"筛选没有包含所有勋章');
    }
    
    if (overlapIds.length > 0) {
      console.log('❌ "我的勋章"和"可获得勋章"有重叠');
      console.log('   可能原因: "可获得勋章"筛选包含了用户已拥有的勋章');
    }
    
    if (potentialInOwned > 0) {
      console.log('❌ "我的勋章"中包含未获得勋章');
      console.log('   可能原因: 筛选逻辑错误，将未获得勋章加入"我的勋章"');
    }
    
    if (potentialInAll === 0 && potentialInAvailable > 0) {
      console.log('❌ "全部勋章"中缺少未获得勋章');
      console.log('   可能原因: "全部勋章"筛选没有包含潜在可获得勋章');
    }
    
  } catch (error) {
    console.error('❌ 调试过程出错:', error.message);
    if (error.response) {
      console.error('详细错误:', error.response.data);
    }
  }
})(); 